package com.easywan.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.easywan.core.JSPModelAndView;
import com.easywan.core.RedirectModelAndView;
import com.easywan.service.ICjPlansService;
import com.easywan.service.IPlansService;
import com.easywan.util.Utils;
import com.easywan.vo.CjPlans;
import com.easywan.vo.Pages;
import com.easywan.vo.Plans;

@Controller
@RequestMapping("admin/CjPlans")
public class CjPlansControllerAdmin {
	private static Logger logger = Logger.getLogger(CjPlansControllerAdmin.class);
	@Autowired 
	ICjPlansService service;
	@Autowired
	IPlansService IPlansService;
	@RequestMapping("index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,Pages pages,
			@RequestParam("pname")String pname,
			@RequestParam("name")String name){
		JSPModelAndView view = new JSPModelAndView("/admin/CjPlans/index");
		CjPlans tempCjPlans = new CjPlans();
		tempCjPlans.setPage(pages);
		List<CjPlans> lists = service.list(tempCjPlans);
		view.addObject("data",lists);
		view.addObject("pname", pname);
		view.addObject("name", name);
		view.addObject("page", pages);
		return view;
	}
	@RequestMapping("add")
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response,
			@RequestParam("pname")String pname,@RequestParam("name")String name){
		JSPModelAndView view = new JSPModelAndView("/admin/CjPlans/modify");
		view.addObject("pname", pname);
		view.addObject("name", name);
		view.addObject("action", "add");
		return view;
	}
	@RequestMapping("add_chk")
	public ModelAndView add_chk(HttpServletRequest request,HttpServletResponse response,CjPlans CjPlans){
		ModelAndView view = new RedirectModelAndView("admin/CjPlans/index");
		service.add(CjPlans);
		view.addObject("pname","");
		view.addObject("name","");
		return view;
	}
	
	@RequestMapping("modify")
	public ModelAndView modify(HttpServletRequest request,HttpServletResponse response,
			@RequestParam("pname")String pname,@RequestParam("name")String name,
			@RequestParam("id")Integer id){
		CjPlans CjPlans = service.findById(id);
		JSPModelAndView view = null;
		view = new JSPModelAndView("/admin/CjPlans/modify");
		view.addObject("pname", pname);
		view.addObject("name", name);
		view.addObject("action", "modify");
		view.addObject("data",CjPlans);
		return view;
	}
	@RequestMapping("modify_chk")
	public ModelAndView modify_chk(HttpServletRequest request,HttpServletResponse response,
			CjPlans CjPlans,@RequestParam("apply") Boolean apply){
		ModelAndView view = new RedirectModelAndView("admin/CjPlans/index");
		if(apply){
			Plans model = new Plans();
			model.setTitle(CjPlans.getTitle());
			model.setContent(CjPlans.getContent());
			model.setSource(CjPlans.getSource());
			CjPlans = service.findById(CjPlans.getId());
			model.setAuthor(CjPlans.getAuthor());
			model.setCover(CjPlans.getCover());
			model.setKeywords(CjPlans.getKeywords());
			
			/*model.setUid(1);
			model.setAid(CjPlans.getId());*/
			IPlansService.add(model);
			service.deleteById(CjPlans.getId());
		}else{
			service.modify(CjPlans);
		}
		view.addObject("pname","");
		view.addObject("name","");
		return view;
	}
	@RequestMapping("del")
	public void del(HttpServletRequest request,HttpServletResponse response,@RequestParam("id")Integer id){
		service.deleteById(id);
		Utils.sendSuccessMsg(response);
	}
}
